package com.kdgc.power.business.service;

import com.kdgc.power.business.entity.ComDict;
import com.kdgc.power.business.entity.ComDictCat;
import com.kdgc.power.business.mapper.ComDictMapper;
import com.kdgc.power.common.util.StringUtil;
import com.kdgc.power.fw.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ComDictCatService extends BaseService<ComDictCat> {

    @Autowired
    private ComDictMapper comDictMapper;
    @Autowired
    private ComDictService comDictService;

    public boolean saveWithDict(ComDictCat comDictCat) {
        Integer catId = comDictCat.getCatId();
        if (StringUtil.isNotEmpty(catId)) {
            ComDictCat oldCat = getEntity(catId);
//            List<ComCDict> dictList = comCDictMapper.createLambdaQuery().andEq("CAT_CODE", oldCat.getCatCode()).andEq("IS_USE", "Y").select();
            List<ComDict> dictList = comDictMapper.findByCatCodeAndIsUse(oldCat.getCatCode(), "Y");
            dictList.forEach(dict -> {
                dict.setCatCode(comDictCat.getCatCode());
                comDictService.saveOrUpdate(dict);
            });
        }
        return saveOrUpdate(comDictCat);
    }
}